package cn.smartorigin.soas.frame.panel;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import cn.smartorigin.soas.data.SqliteConnecter;
import cn.smartorigin.soas.data.TestData;
import cn.smartorigin.soas.frame.AddPlateTypeFrame;
import cn.smartorigin.soas.frame.ColorTableIF;
import cn.smartorigin.soas.frame.ImageButton;
import cn.smartorigin.soas.frame.JColorTable;
import cn.smartorigin.soas.frame.ModifyPlateType;

public class ManagePlateTypePage extends CellPagePanel implements  ColorTableIF{

	JColorTable   cTable;
	JScrollPane mClistPanel;
	
    ImageIcon minimizeIcon = new ImageIcon("res/images/add_platetype.png");
    ImageIcon minimizeIcon_p = new ImageIcon("res/images/add_platetype_p.png");
    ImageIcon printIcon = new ImageIcon("res/images/print.png");
    ImageIcon printIcon_p = new ImageIcon("res/images/print_p.png");
	
	public ManagePlateTypePage(){
		this.setLayout(new BorderLayout());
		addPlateTypeTable();
	}
	
	private DefaultTableModel loadClientDate() {
		DefaultTableModel model = new DefaultTableModel();
	    model.addColumn("序号");
	    model.addColumn("表面处理种类");
	    try {
			TestData.getPlateTypeList(new SqliteConnecter(), model);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	    return model;
		
	}

	private void addPlateTypeTable() {
		DefaultTableModel model = loadClientDate();
	    cTable = new JColorTable(model);
	    mClistPanel= new JScrollPane(cTable);
		this.add(mClistPanel, BorderLayout.CENTER);
		
		JPanel pl = new JPanel();
		//pl.setBackground(Color.white);
		pl.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
        ImageButton addPlateType = new ImageButton(minimizeIcon, minimizeIcon, minimizeIcon_p);
        addPlateType.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				new AddPlateTypeFrame(ManagePlateTypePage.this);
				
			}
        	
        });
		pl.add(addPlateType);
        ImageButton printBtn= new ImageButton(printIcon, printIcon, printIcon_p);
		pl.add(printBtn);
		this.add(pl,BorderLayout.SOUTH);
		
	}
	
	private void flashPlateTypeTable(){
		DefaultTableModel model = loadClientDate();
		cTable.removeAll();
		cTable.setModel(model);
	}
	
	public void flashData(){
		flashPlateTypeTable();
	}

	@Override
	public void TableModify(int rowId) {
		// TODO Auto-generated method stub
		String plateTypeName =  (String)cTable.getValueAt(rowId, 1);
		new ModifyPlateType(plateTypeName, ManagePlateTypePage.this);
	}

	@Override
	public void TableDelete(int rowId) {
		// TODO Auto-generated method stub
		String plateTypeName =  (String)cTable.getValueAt(rowId, 1);
		try {
			TestData.deletePlateType(new SqliteConnecter(), plateTypeName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.flashPlateTypeTable();
		this.repaint();
	}

}
